<template>
  <div class="classic">
    <van-search v-model="value" placeholder="输入搜索关内容" @click="gotoSearch"/>
    <van-sidebar v-model="active">
      <van-sidebar-item v-for="item in classifyList" :key="item.title" :title="item.title" />
    </van-sidebar>
    <main v-if="classifyList">
        <p>当季最热</p>
        <div class="popular">
          <div v-for="item in classifyList[active].children" @click="goSearch(item.title)">
            <img :src="item.img" alt=""/>
            <span>{{ item.title }}</span>
          </div>
        </div>
        <p>其他</p>
        <div class="others">
          <span v-for="item in classifyList[active].other" :key="item"  @click="goSearch(item)">{{ item }}</span>
        </div>
    </main>
  </div>
</template>

<script>
import {computed, onMounted, ref} from 'vue';
// import { getClassifyList } from "../utils/api.js";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
export default {
  setup(){
    const router = useRouter();
    const store = useStore();

    const active = ref(0);
    const value=ref("");

    // let classifyList = ref(null);
    // const getClassify = async () => {
    //   const res = await getClassifyList();
    //   classifyList.value = res.result;
    //   console.log(classifyList);
    // }
    const classifyList = computed(() => store.state.classic.classifyList);
    const getClassify = () => {
      store.dispatch('classic/getClassify');
    };

    onMounted(() => {
      getClassify();
    })

    const gotoSearch = () => {
      router.push("/search");
    }

    const goSearch = (val) => {
      store.dispatch("classic/goSearch", val);
      router.push("/search");
    }

    return { active,value, classifyList, getClassify, gotoSearch, goSearch};
  }
};
</script>
<style lang="scss" scoped>
.classic{
  position:relative;
  main{
    padding-top:20px;
    position:absolute;
    top:54px;
    left:80px;
    width:295px;
    p{
      font-size:16px;
      font-weight:900;
      text-align: left;
      padding-left:10px;
    }
    .popular{
    display:flex;
    flex-wrap: wrap;
    justify-content: space-around;
    text-align:center;
    div{
      width:30%;
      height:100px;
      font-size:14px;
      img{
        height:65px;
        width:90%;
      }

    }
    }
    .others{
      display:flex;
      justify-content: space-around;
      flex-wrap:wrap;
      font-size:14px;
      span{
        display: inline-block;
        width:30%;
        height:40px;
        line-height:40px;
        text-align:center;
        border:solid 1px rgb(19, 180, 180);
        border-radius:8px;
        margin:10px 0;
      }
    }
    
  }
}
</style>